package com.um.jdy.common.commons.service.impl;

import com.um.jdy.common.charge.dao.EboxOrderDao;
import com.um.jdy.common.charge.dao.EvOrderDao;
import com.um.jdy.common.charge.entity.po.EboxOrder;
import com.um.jdy.common.charge.entity.po.EvOrder;
import com.um.jdy.common.charge.enums.OrderEnum;
import com.um.jdy.common.commons.service.StatusService;
import com.um.jdy.common.member.enums.PayEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 邓南辉
 * 日期 2023/7/6
 * 版权所有 广州优匠科技有限公司
 */
@Service
@Slf4j
public class StatusServiceImpl implements StatusService {

    @Autowired
    EboxOrderDao eboxOrderDao;

    @Autowired
    EvOrderDao evOrderDao;

    /**
     * 返回订单状态
     * @param status
     * @param payStatus
     * @return
     */
    @Override
    public OrderEnum.OrderStatus getOrderStatus(String orderId,String status,String payStatus,String deviceType) {
        if(status.equals(OrderEnum.Status.Charging.name())){
            return OrderEnum.OrderStatus.Doing;
        }else if(status.equals(OrderEnum.Status.Completed.name()) && payStatus.equals(PayEnum.Status.Payed.name())){
            return OrderEnum.OrderStatus.Completed;
        }else if(status.equals(OrderEnum.Status.Completed.name()) && payStatus.equals(PayEnum.Status.UnPayed.name())){
            if(deviceType.equals("EBox") || deviceType.equals("EV")) {

                String placeStatus = "";
                if(deviceType.equals("EBox")) {
                    EboxOrder eboxOrder = eboxOrderDao.getEntity("charge_order_id", orderId);
                    placeStatus = eboxOrder != null ? eboxOrder.getPlaceStatus() : "";
                }

                if(deviceType.equals("EV")) {
                    EvOrder evOrder = evOrderDao.getEntity("charge_order_id", orderId);
                    placeStatus = evOrder != null ? evOrder.getPlaceStatus() : "";
                }

                //如果正在占位中，说明已经结束充电，正在占位
                if (placeStatus.equals(OrderEnum.PlaceStatus.Place.name())) {
                    return OrderEnum.OrderStatus.Place;
                }
            }
            return OrderEnum.OrderStatus.UnPay;
        }else {
            return OrderEnum.OrderStatus.NotStarted;
        }
    }
}
